from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,BadData
from mall import settings


def generate_safe_token(openid):
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    access_token = serializer.dumps(openid)

    return access_token.decode()


def check_safe_token(token):
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        openid = serializer.loads(token)
    except BadData:
        openid = None

    return openid
